package com.ffzx.commerce.framework.system.entity;

import java.util.List;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;

import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.Length;

import com.ffzx.commerce.framework.common.persistence.DataEntity;
import com.ffzx.commerce.framework.utils.CollectionsUtils;
import com.ffzx.commerce.framework.utils.Global;
import com.google.common.collect.Lists;

/**
 * 用户Entity
 * 
 * @author zhugj
 * @date 2016年3月1日 下午2:03:29
 * @version 0.1.0
 */
public class SysUser extends DataEntity<SysUser> {

	private static final long serialVersionUID = 1L;
	@NotNull(message="归属公司不能为空")
	private SysOffice company;	// 归属公司
	@NotNull(message="归属部门不能为空")
	private SysOffice office;	// 归属部门
    @NotNull
	private String loginName;// 登录名
	private String password;// 密码
    @NotNull
	private String workNo;		// 工号
    @NotNull
	private String name;	// 姓名
	@Email(message="邮箱格式不正确")
	@Length(min=0, max=200, message="邮箱长度必须介于 1 和 200 之间")
	private String email;	// 邮箱
	private String phone;	// 电话
	@NotNull
    @Pattern(regexp="^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\\d{8})$",message="请输入正确的手机号")	
	private String mobile;	// 手机
    @NotNull
	private String userType;// 用户类型
	/**
	 * 是否允许登陆
	 * @see Global.YES  是     Global.NO  否
	 */
	private String loginFlag;

	private SysRole role;	// 根据角色查询用户条件
	
	private List<SysRole> roleList = Lists.newArrayList(); // 拥有角色列表

	public SysUser() {
		super();
	}
	
	public SysUser(String id){
		super(id);
	}

	public SysUser(String id, String loginName){
		super(id);
		this.loginName = loginName;
	}

	public SysUser(SysRole role){
		super();
		this.role = role;
	}
	
	public String getLoginFlag() {
		return loginFlag;
	}

	public void setLoginFlag(String loginFlag) {
		this.loginFlag = loginFlag;
	}

	public String getId() {
		return id;
	}

	public SysOffice getCompany() {
		return company;
	}

	public void setCompany(SysOffice company) {
		this.company = company;
	}
	
	public SysOffice getOffice() {
		return office;
	}

	public void setOffice(SysOffice office) {
		this.office = office;
	}

	public String getLoginName() {
		return loginName;
	}

	public void setLoginName(String loginName) {
		this.loginName = loginName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getName() {
		return name;
	}
	
	public String getWorkNo() {
		return workNo;
	}

	public void setWorkNo(String workNo) {
		this.workNo = workNo;
	}

	public void setName(String name) {
		this.name = name;
	}
	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}
	
	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}
	public String getMobile() {
		return mobile;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}
	
	public String getUserType() {
		return userType;
	}

	public void setUserType(String userType) {
		this.userType = userType;
	}

	public SysRole getRole() {
		return role;
	}

	public void setRole(SysRole role) {
		this.role = role;
	}

	public List<SysRole> getRoleList() {
		return roleList;
	}
	
	public void setRoleList(List<SysRole> roleList) {
		this.roleList = roleList;
	}

	public List<String> getRoleIdList() {
		List<String> roleIdList = Lists.newArrayList();
		for (SysRole role : roleList) {
			roleIdList.add(role.getId());
		}
		return roleIdList;
	}

	public void setRoleIdList(List<String> roleIdList) {
		roleList = Lists.newArrayList();
		for (String roleId : roleIdList) {
			SysRole role = new SysRole();
			role.setId(roleId);
			roleList.add(role);
		}
	}
	
	/**
	 * 用户拥有的角色名称字符串, 多个角色名称用','分隔.
	 */
	public String getRoleNames() {
		return CollectionsUtils.extractToString(roleList, "name", ",");
	}
	
	public boolean isAdmin(){
		return isAdmin(this.id);
	}
	
	public static boolean isAdmin(String id){
		return id != null && "1".equals(id);
	}
	
	@Override
	public String toString() {
		return id;
	}
}
